#include<cstdio>
#include<windows.h>
#include<ctime>
#include<conio.h>
#include<thread>

#define cls system("cls")

using namespace std;

bool life[2][10005][10005];

bool time_r=0;


inline void endg(const int n){for(int i=1;i<=n;i++)putchar(' '); }

inline void endn(const int n){for(int i=1;i<=n;i++)putchar('\n');}

inline void white_bound(const char str){printf("\033[47;30m%s\033[0m",str);}

inline size_t read(){
	size_t x = 0, f = 1;
	char c = getchar();
	while(c < '0' || c > '9'){
		if(c == '-'){
			f = -1;
		}
		c = getchar();
	}
	while(c >= '0' && c <= '9'){
		x = x*10+c-'0';
		c = getchar();
	}
	return x*f;
}

void write(size_t x)
{
    if(x<0)
    {
    putchar('-');
    x=-x;
    }
    if(x>=10)
        write(x/10);
    putchar(x%10+'0');
}

inline void show(int x,int y)
{
	cls;
	endn(1);
	for(int i=1;i<=x;i++)
	{
		endg(3);
		for(int j=1;j<=y;j++)
		{
			write(life[!time_r][i][j]);
			putchar(32);
		}
		putchar('\n');
	}
}

inline void basic_ready(int x,int y)
{
	endn(1);
	for(int i=1;i<=x;i++)
	{
		endg(3);
		for(int j=1;j<=y;j++)
		{
			life[0][i][j]=rand()%2;
			write(life[0][i][j]);
			putchar(32);
		}
		endn(1);
	}
}

inline void Start_once(int,int);


int main()
{
	srand(int(time(0)));
	int n,m;
	char clin;
	scanf("%d%d",&n,&m);
	cls;
	
	basic_ready(n,m);
	endn(2);
	endg(10);
	printf("Start:");
	
	
	while(1)
	{
		getch();
		Start_once(n,m);
		show(n,m);
		time_r=!time_r;
	}
	
	return 0;
}

inline void Start_once(int x,int y)
{
	for(int i=1;i<=x;i++)
	{
		for(int j=1;j<=y;j++)
		{
			int sum=0;
			
			if(life[time_r][i-1][j-1]==1)sum++;
			if(life[time_r][i-1][j]==1)sum++;
			if(life[time_r][i-1][j+1]==1)sum++;
			if(life[time_r][i][j-1]==1)sum++;
			if(life[time_r][i][j+1]==1)sum++;
			if(life[time_r][i+1][j-1]==1)sum++;
			if(life[time_r][i+1][j]==1)sum++;
			if(life[time_r][i+1][j+1]==1)sum++;
			
			if(life[time_r][i][j]==1)
			{
				if(sum<2||sum>3)life[!time_r][i][j]=0;
			}
			else if(life[time_r][i][j]==0)
			{
				if(sum==3)life[!time_r][i][j]=1;
			}
		}
	}
}
